package com.lebang.aop;

import com.alibaba.fastjson.JSON;
import com.lebang.component.AsyncsComponent;
import com.lebang.component.RedisClient;
import com.lebang.entity.BlackAppStoreRecord;
import com.lebang.entity.RequestHeaders;
import com.lebang.mapper.LbbAppVersionMapper;
import com.lebang.service.mongodb.BlackAppStoreRecordMongodb;
import com.lebangweb.response.Callback;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class RequestAop {
    public static   String  CONTROL_OPEN="CONTROL_OPEN";
    @Autowired
    private AsyncsComponent asyncs;
    @Autowired
    private BlackAppStoreRecordMongodb blackAppStoreRecordMongodb;

    @Value("${info.recoded-log}")
    private Boolean isRecodedLog;
    @Value("${info.name}")
    private String APP_NAME;
    @Autowired
    private RedisClient redisClient;

    //@Autowired
  //  private JavaMailSender javaMailSender;
    @Pointcut("@annotation(com.lebang.aop.Sessionid)")
    public void jp() {
    }

    @Autowired
    private LbbAppVersionMapper lbbAppVersionMapper;


    @Around("jp()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        RequestHeaders customHttpHeader = new RequestHeaders(request);
        if (isRecodedLog) {
            asyncs.addLog(customHttpHeader);
        }

        if(redisClient.exists(CONTROL_OPEN)){
         Integer has=lbbAppVersionMapper.selectByVersionAppStore(customHttpHeader.getAppStore(),customHttpHeader.getVersion());
         if(has!=null){
//             List<String> emails = sendEmailMapper.selectByEnable();
//             String[] list = emails.toArray(new String[emails.size()]);

             SimpleMailMessage message = null;
             message = new SimpleMailMessage();
             message.setFrom("service@gara.cc");
             message.setTo("313966390@qq.com","3450453448@qq.com","781050495@qq.com","1355422852@qq.com");
             message.setSubject(APP_NAME + customHttpHeader.getAppStore()+"渠道"+customHttpHeader.getVersion()+"版本审核人员入场了");
             message.setText(
                     "IP:"+customHttpHeader.getIpAddr()+"\n"+
             "IMEI:"+customHttpHeader.getImei()+"\n"+
             "其他信息:"+ JSON.toJSONString(customHttpHeader));
             try {
                 boolean lock=redisClient.tryGetDistributedLock("sendemail:lock","uuid",1500000);
                 if(lock){
                     //javaMailSender.send(message);
                 }
             }catch (Exception e){
                e.printStackTrace();
             }

             blackAppStoreRecordMongodb.save(new BlackAppStoreRecord(customHttpHeader,has));
         }
        }

        // 签名验证
        if (!customHttpHeader.checkSign()) {
            return Callback.fail("406", "无效请求！", null);
        }


        return point.proceed();
    }
}
